package com.glsc.ngateway.opmanage.service.ledger;


import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.archive.CounterParty;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.ledger.LedgerAuthStaff;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.ledger.LedgerAuthTemplate;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.ledger.LedgerTemplate;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.ledger.LedgerAuthTemplateRepository;
import com.glsc.ngateway.common.base.repo.mysql.opmanagemain.ledger.LedgerTemplateRepository;
import com.glsc.ngateway.opmanage.utils.SpecificationUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class LedgerAuthTemplateService {

    @Resource
    private LedgerAuthTemplateRepository ledgerAuthTemplateRepository;

    @Resource
    private LedgerTemplateRepository ledgerTemplateRepository;

    @Resource
    private LedgerAuthStaffService ledgerAuthStaffService;


    /**
     * 查出该用户所有的岗位信息
     *
     * @param userOaAccount
     * @return
     */
    public List<LedgerAuthTemplate> listByUserId(String userOaAccount) {
        List<LedgerAuthStaff> ledgerAuthStaffs = ledgerAuthStaffService.listByUserOaAccount(userOaAccount);
        if (CollectionUtils.isEmpty(ledgerAuthStaffs)) {
            return null;
        }
        List<LedgerAuthTemplate> ledgerAuthTemplates = listByAuthIds(ledgerAuthStaffs.stream().map(LedgerAuthStaff::getLedgerAuthId).collect(Collectors.toList()));
        return ledgerAuthTemplates;
    }

    /**
     * 分页查找
     *
     * @param pageNo
     * @param pageSize
     * @param param
     * @return
     */
    public Page<LedgerAuthTemplate> page(int pageNo, int pageSize, Map<String, Object> param) {
        Sort.Direction direction = Sort.Direction.DESC;
        String sort = "createTime";

        pageNo = PageUtil.initPageNo(pageNo);
        pageSize = PageUtil.initPageSize(pageSize);

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize, direction, sort);
        Page<LedgerAuthTemplate> page = ledgerAuthTemplateRepository.findAll(SpecificationUtil.buildSpecification(param, LedgerAuthTemplate.class), pageable);

        return page;
    }

    /**
     * 根据id查找台账模板信息
     *
     * @param authId
     * @return
     */
    public List<LedgerAuthTemplate> listByAuthId(Integer authId) {
        List<LedgerAuthTemplate> list = ledgerAuthTemplateRepository.findAll(Example.of(LedgerAuthTemplate.builder().ledgerAuthId(authId).build()));
        return list;
    }

    /**
     * 根据权限id集合查询所有的台账模板信息
     *
     * @param authIds
     * @return
     */
    public List<LedgerAuthTemplate> listByAuthIds(List authIds) {
        if (CollectionUtils.isEmpty(authIds)) {
            return null;
        }

        Map<String, Object> param = new HashMap<>();
        param.put("IN_ledgerAuthId", StringUtils.join(authIds, ","));

        List<LedgerAuthTemplate> list = ledgerAuthTemplateRepository.findAll(SpecificationUtil.buildSpecification(param, LedgerAuthTemplate.class));
        return list;
    }


    /**
     * 根据id查找
     *
     * @param id
     * @return
     */
    public LedgerAuthTemplate findById(Integer id) {
        Optional<LedgerAuthTemplate> optional = ledgerAuthTemplateRepository.findById(id);
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 保存
     *
     * @param obj
     * @return
     */
    public LedgerAuthTemplate save(LedgerAuthTemplate obj) {

        //不能重复新增
        List<LedgerAuthTemplate> templates = ledgerAuthTemplateRepository.findAll(Example.of(LedgerAuthTemplate.builder().ledgerAuthId(obj.getLedgerAuthId())
                .ledgerTemplateId(obj.getLedgerTemplateId()).build()));
        if (CollectionUtils.isNotEmpty(templates)) {
            obj.setId(templates.get(0).getId());
        }

        Optional<LedgerTemplate> optional = ledgerTemplateRepository.findById(obj.getLedgerTemplateId());
        if (optional.isPresent()) {
            obj.setLedgerName(optional.get().getName());
            obj.setLedgerTemplateId(optional.get().getId());
        }

        LedgerAuthTemplate res = ledgerAuthTemplateRepository.save(obj);
        return res;
    }


    /**
     * 根据id删除
     *
     * @param id
     */
    public void deleteById(Integer id) {
        ledgerAuthTemplateRepository.deleteById(id);
    }

    /**
     * 根据id批量删除
     *
     * @param ids
     */
    public void deleteByIds(List<Integer> ids) {
        ledgerAuthTemplateRepository.deleteAllById(ids);
    }


}
